package com.ev.apis.controller.asl;

import com.ev.apis.model.DsResultResponse;
import com.ev.asl.constant.AslConstant;
import com.ev.asl.service.CheckStatisticsService;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 检查统计表Api
 * @author 姚家乐
 * @since 2021/6/30 8:48
 */
@Api(value = "/", tags = "检查统计表api")
@RestController
public class CheckStatisticsApiController {

    @Autowired
    private CheckStatisticsService service;

    @EvApiByToken(value = "/apis/asl/CheckStatistics/query", method = RequestMethod.POST, apiTitle = "检查统计表分析数据")
    @ApiOperation("检查统计表分析数据")
    public R accounting(@ApiParam(value = "刪除状态") @RequestParam(value = "delFlag", defaultValue = "", required = false) Integer delFlag,
                        @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno", defaultValue = "1") int pageno,
                        @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize", defaultValue = "20") int pagesize,
                        @ApiParam(value = "日期的起始时间") @RequestParam(value = "billDateStart", defaultValue = "", required = false) Date billDateStart,
                        @ApiParam(value = "日期的结束时间") @RequestParam(value = "billDateEnd", defaultValue = "", required = false) Date billDateEnd,
                        @ApiParam(value = "检查结果（0：合格，1：不合格）") @RequestParam(value = "result", defaultValue = "", required = false) Integer result,
                        @ApiParam(value = "品名") @RequestParam(value = "product", defaultValue = "", required = false) String product) {
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(5);
        param.put("offset", (pageno - AslConstant.ONE) * pagesize);
        param.put("limit", pagesize);
        param.put("product", product);
        param.put("over", 1);
        param.put("billDateStart", billDateStart);
        param.put("billDateEnd", billDateEnd);
        param.put("result", result);

        Integer total = service.getCountInfoList(param);
        if (total > AslConstant.ZERO){
            Map<String, Object> results = Maps.newHashMapWithExpectedSize(2);
//            Map<String,Object> data = service.query(param);
            Map<String,Object> data = service.query2(param);
            List<Map<String,Object>> list = (List<Map<String, Object>>) data.get("data");
            results.put("data", new DsResultResponse(pageno, pagesize, total, list));
            results.put("size",data.get("size"));
            return R.ok(results);
        }
        return R.ok();
    }

}
